//
//  UIButton+Extension.swift
//  LSTools
//
//  Created by zrf on 2021/6/30.
//

import Foundation
import AVKit

public enum ImageTitleAlignment {
    case top, left, bottom, right, center
}

public extension UIButton {
    
    /**
     @brief 跳转图片与文字排版。 系统默认图片在左，文字在右
     @param alignment 图片所在位置
     @param spacing 图片与文件间距
     */
    func adjustImageTitlePosition(_ alignment: ImageTitleAlignment, spacing: CGFloat) {
        
        
        self.sizeToFit()
        
        let imageWidth = self.imageView?.image?.size.width
        let imageHeight = self.imageView?.image?.size.height
        
        let labelWidth = self.titleLabel?.intrinsicContentSize.width
        let labelHeight = self.titleLabel?.intrinsicContentSize.height
        
        switch alignment {
        case .center:
            imageEdgeInsets = UIEdgeInsets(top: -labelHeight!-spacing/2, left: imageWidth! / 2, bottom: 0, right: 0)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth!, bottom: -imageHeight!-spacing/2, right: 0)
            break
        case .top:
            imageEdgeInsets = UIEdgeInsets(top: -labelHeight!-spacing/2, left: 0, bottom: 0, right: -labelWidth!)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth!, bottom: -imageHeight!-spacing/2, right: 0)
            break
            
        case .left:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: -spacing/2, bottom: 0, right: spacing)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: spacing/2, bottom: 0, right: -spacing/2)
            break
            
        case .bottom:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -labelHeight!-spacing/2, right: -labelWidth!)
            titleEdgeInsets = UIEdgeInsets(top: -imageHeight!-spacing/2, left: -imageWidth!, bottom: 0, right: 0)
            break
            
        case .right:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: labelWidth!+spacing/2, bottom: 0, right: -labelWidth!-spacing/2)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth!-spacing/2, bottom: 0, right: imageWidth!+spacing/2)
            break
        }
      
    }
    
    
    /**
     @brief 设置文字与图片间距
     */
    func setupButtonImageAndTitlePossitionWith(padding: CGFloat, style: ImageTitleAlignment){
            let imageRect: CGRect = self.imageView?.frame ?? CGRect.init()
            let titleRect: CGRect = self.titleLabel?.frame ?? CGRect.init()
            let selfWidth: CGFloat = self.frame.size.width
            let selfHeight: CGFloat = self.frame.size.height
            let totalHeight = titleRect.size.height + padding + imageRect.size.height
            switch style {
            case .left:
                self.titleEdgeInsets = UIEdgeInsets(top: 0, left: padding / 2, bottom: 0, right: -padding / 2)
                self.imageEdgeInsets = UIEdgeInsets(top: 0, left: -padding / 2, bottom: 0, right: padding / 2)
            case .right:
                self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -(imageRect.size.width + padding/2), bottom: 0, right: (imageRect.size.width + padding/2))
                self.imageEdgeInsets = UIEdgeInsets(top: 0, left: (titleRect.size.width + padding / 2), bottom: 0, right: -(titleRect.size.width +  padding/2))
            case .top :
                self.titleEdgeInsets = UIEdgeInsets(top: ((selfHeight - totalHeight) / 2 + imageRect.size.height + padding - titleRect.origin.y), left: (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2, bottom: -((selfHeight - totalHeight) / 2 + imageRect.size.height + padding - titleRect.origin.y), right: -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2)
                self.imageEdgeInsets = UIEdgeInsets(top: ((selfHeight - totalHeight) / 2 - imageRect.origin.y), left: (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2), bottom: -((selfHeight - totalHeight) / 2 - imageRect.origin.y), right: -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2))
            case .bottom:
                self.titleEdgeInsets = UIEdgeInsets(top: ((selfHeight - totalHeight) / 2 - titleRect.origin.y), left: (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2, bottom: -((selfHeight - totalHeight) / 2 - titleRect.origin.y), right: -(selfWidth/2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2)
                self.imageEdgeInsets = UIEdgeInsets(top: ((selfHeight - totalHeight) / 2 + titleRect.size.height + padding - imageRect.origin.y), left: (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2), bottom: -((selfHeight - totalHeight) / 2 + titleRect.size.height + padding - imageRect.origin.y), right: -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2))
            default:
                self.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
                self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            }
    }
    func setVideoImage(_ url: URL){
        DispatchQueue.global().async {
            let asset = AVURLAsset(url: url)
            let generator = AVAssetImageGenerator(asset: asset)
            generator.appliesPreferredTrackTransform = true
            let time = CMTimeMakeWithSeconds(1.0, preferredTimescale: 600)
            
            var actualTime: CMTime = CMTime.zero
            let imageRef = try? generator.copyCGImage(at: time, actualTime: &actualTime)
            if imageRef != nil {
                let img = UIImage(cgImage: imageRef!)
                
                DispatchQueue.main.async {
                    self.setImage(img, for: .normal)
                    
                }
            }
                
        }
    }
}

